<template>
  <section class="ling-top-text improvement_personal">
    <h2>实名认证</h2>
    <div class="improvement_personal_detail">
      <div class="improvement_personal_items">
        <div class="certificate_items" style="border-bottom: none; margin-top: 0">
          <div class="uploader ling-flex">
            <span>身份证正面</span>
            <van-uploader
              v-model="fileList.idNumberPositiveUrl"
              :after-read="(file)=>afterRead(file,'idNumberPositiveUrl')"
              :max-count="1"
            />
          </div>
        </div>
      </div>

      <div class="improvement_personal_items">
        <div class="certificate_items" style="border-bottom: none; margin-top: 0">
          <div class="uploader ling-flex">
            <span>身份证反面</span>
            <van-uploader
              v-model="fileList.idNumberDorsalUrl"
              :after-read="(file)=>afterRead(file,'idNumberDorsalUrl')"
              :max-count="1"
            />
          </div>
        </div>
      </div>

      <div class="improvement_personal_items">
        <van-field
          input-align="right"
          v-model="fileList.bankAccount"
          clearable
          label="银行卡号"
          name="isBankCard"
          maxlength="20"
          v-validate="'isBankCard'"
          :error="errors.has('isBankCard')"
          :error-message="errors.first('isBankCard')"
        />
      </div>

      <div class="improvement_personal_items">
        <div class="certificate_items" style="border-bottom: none; margin-top: 0">
          <div class="uploader ling-flex">
            <span>银行卡正面</span>
            <van-uploader
              v-model="fileList.bankAccountUrl"
              :after-read="(file)=>afterRead(file,'bankAccountUrl')"
              :max-count="1"
            />
          </div>
        </div>
      </div>

      <div class="ling-linearButton improvement_btn" @click="handleSubmit">提交</div>
    </div>
  </section>
</template>

<script>
import { uploadToQiniu } from '@/utils/qiniuUpload'
import {
  createNamespacedHelpers,
  mapState as rootState,
  mapActions as rootActions,
} from 'vuex'
const { mapState, mapActions } = createNamespacedHelpers('myData')
import { getRealName } from '@/utils/userAuth'
export default {
  name: 'improvementPersonalPage',
  data() {
    return {
      isLeave: false,
      bankAccountUrl: [],
      fileList: [],
    }
  },
  created() {
    this.fileList = this.realName
  },
  components: {},
  computed: {
    realName() {
      const {
        idNumberPositiveUrl,
        idNumberDorsalUrl,
        bankAccount,
        bankAccountUrl,
      } = JSON.parse(getRealName())
      return {
        idNumberPositiveUrl: idNumberPositiveUrl
          ? [{ url: idNumberPositiveUrl }]
          : [],
        idNumberDorsalUrl: idNumberDorsalUrl
          ? [{ url: idNumberDorsalUrl }]
          : [],
        bankAccount,
        bankAccountUrl: bankAccountUrl ? [{ url: bankAccountUrl }] : [],
      }
    },
  },
  iconName() {
    return `#icon-${this.iconClass}`
  },
  methods: {
    ...mapActions(['editRealName', 'getUserInfo']),
    ...rootActions(['getUserInfo']),
    handleSubmit() {
      this.$validator.validateAll().then((result) => {
        const {
          bankAccount,
          bankAccountUrl,
          idNumberDorsalUrl,
          idNumberPositiveUrl,
        } = this.realName
        if (result) {
          this.$dialog
            .confirm({
              message: '是否确认提交？',
            })
            .then(() => {
              this.editRealName({
                bankAccount,
                bankAccountUrl: bankAccountUrl[0] ? bankAccountUrl[0].url : '',
                idNumberDorsalUrl: idNumberDorsalUrl[0]
                  ? idNumberDorsalUrl[0].url
                  : '',
                idNumberPositiveUrl: idNumberPositiveUrl[0]
                  ? idNumberPositiveUrl[0].url
                  : '',
                userId: JSON.parse(getRealName()).userId,
              }).then(async (res) => {
                this.isLeave = true
                await this.getUserInfo()
                this.$router.go(-1)
              })
            })
        }
      })
    },

    afterRead(file, type) {
      file.status = 'uploading'
      file.message = '上传中...'
      uploadToQiniu(file.file, () => {})
        .then((response) => {
          file.status = 'done'
          const states = { [type]: response }
          this.fileList[type] = [{ url: response }]
        })
        .catch((e) => {
          console.log('e: ', e)
          file.status = 'failed'
          file.message = '上传失败'
        })
    },
  },
  // beforeRouteLeave(to, from, next) {
  //   // 导航离开该组件的对应路由时调用
  //   // 该页面不可返回上一个页面，但是可以通过触发handleSubmit方法返回
  //   if (from.path == '/improvementPersonal' && !this.isLeave) {
  //     next(false)
  //   } else {
  //     next()
  //   }
  // }
}
</script>

<style lang="less" scoped>
.improvement_personal {
  position: relative;
  .improvement_personal_detail {
    padding-left: 20px;
    position: relative;
    .improvement_personal_items,
    .certificate_items {
      border-bottom: solid 1px #dddddd;
      margin-top: 18px;
      position: relative;
      .van-cell {
        padding-top: 0;
      }
      .uploader {
        line-height: 45px;
        padding: 0 15px;
        input {
          width: 40px;
          height: 26px;
          opacity: 0;
          z-index: 1;
          position: absolute;
          right: 10px;
          overflow: hidden;
        }
        span {
          color: #646566;
          font-size: 14px;
        }

        i {
          font-size: 10px;
          color: #7ac5ef;
          font-style: normal;
          z-index: 0;
        }
      }
      .certificate {
        width: 95px;
        height: 60px;
        overflow: hidden;
        margin-right: 10px;
        margin-bottom: 10px;
        display: inline-block;
        background-size: cover;
        background-position: center center;
      }
    }
  }
  .van-uploader {
    margin-right: 12px !important;
  }
}
</style>
